#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname extra $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion extra $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive extra $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
echo "APPENDDEF(`confENVDEF',`-DSTARTTLS -DSASL -DLDAPMAP')
APPENDDEF(`confLIBS', `-lssl -lcrypto -lsasl2 -lldap -llber -ldb')
APPENDDEF(`confINCDIRS', `-I/usr/include/sasl')
" > devtools/Site/site.config.m4 && \
echo "define(`confMANGRP',`root')
define(`confMANOWN',`root')
define(`confSBINGRP',`root')
define(`confUBINGRP',`root')
define(`confUBINOWN',`root')
" > devtools/Site/site.config.m4 && \
sed -i 's|/usr/man/man|/usr/share/man/man|' \
    devtools/OS/Linux           && \
sed -i -r "s/^# if (DB.*)$/# if (\1) || DB_VERSION_MAJOR >= 5/" \
    include/sm/bdb.h


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
cd sendmail                     && \
    sh Build                        && \
    cd ../cf/cf                     && \
    cp generic-linux.mc sendmail.mc && \
    sh Build sendmail.cf && \
    rm -vRf $PKG_DEST/inst && \
    mkdir -pv $PKG_DEST/inst/{usr/{bin,sbin,share/{man/{man1,man5,man8},doc},etc,bin,sbin} && \
    install -v -d -m755 $PKG_DEST/inst/etc/mail && \
    sh Build -E DESTDIR=$PKG_DEST/inst install-cf && \
    cd ../..            && \
    sh Build -E DESTDIR=$PKG_DEST/inst install    && \
    install -v -m644 cf/cf/{submit,sendmail}.mc $PKG_DEST/inst/etc/mail && \
    cp -v -R cf/* $PKG_DEST/inst/etc/mail                               && \
    install -v -m755 -d $PKG_DEST/inst/usr/share/doc/$PKG_NAME-$PKG_VERSION/{cf,sendmail} && \
    install -v -m644 CACerts FAQ KNOWNBUGS LICENSE PGPKEYS README RELEASE_NOTES \
            $PKG_DEST/inst/usr/share/doc/$PKG_NAME-$PKG_VERSION && \
    install -v -m644 sendmail/{README,SECURITY,TRACEFLAGS,TUNING} \
            $PKG_DEST/inst/usr/share/doc/$PKG_NAME-$PKG_VERSION/sendmail && \
    install -v -m644 cf/README $PKG_DEST/inst/usr/share/doc/$PKG_NAME-$PKG_VERSION/cf && \
    for manpage in sendmail editmap mailstats makemap praliases smrsh
    do
        install -v -m644 $manpage/$manpage.8 $PKG_DEST/inst/usr/share/man/man8 || exit 10;
    done && \
    install -v -m644 sendmail/aliases.5    $PKG_DEST/inst/usr/share/man/man5 && \
    install -v -m644 sendmail/mailq.1      $PKG_DEST/inst/usr/share/man/man1 && \
    install -v -m644 sendmail/newaliases.1 $PKG_DEST/inst/usr/share/man/man1 && \
    install -v -m644 vacation/vacation.1   $PKG_DEST/inst/usr/share/man/man1 && \
    cd doc/op                                       && \
    sed -i 's/groff/GROFF_NO_SGR=1 groff/' Makefile && \
    make op.txt && \
    install -v -d -m755 /usr/share/doc/$PKG_NAME-$PKG_VERSION && \
    install -v -m644 op.txt /usr/share/doc/$PKG_NAME-$PKG_VERSION && \
    cd ../..



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
# BLK3



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/extra/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/extra/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
errcho "Build complete extra/$PACKAGE"
errcho "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
